package com.xique.park.bean.transform.web.subDev;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xique.park.bean.domain.ParkDeviceSub;
import com.xique.park.bean.vo.web.subDev.ParkSubDevVo;
import lombok.experimental.UtilityClass;

/**
 * @author liuxueli
 */
@UtilityClass
public class ParkSubDevTransform {

    /**
     * 数据库实体转化为前端vo
     */
    public static ParkSubDevVo transformParkSubDevVo(ParkDeviceSub parkDeviceSub) {
        ParkSubDevVo parkSubDevVo = new ParkSubDevVo();
        parkSubDevVo.setId(parkDeviceSub.getId());
        parkSubDevVo.setLaneId(parkDeviceSub.getLaneId());
        parkSubDevVo.setName(parkDeviceSub.getName());
        parkSubDevVo.setType(parkDeviceSub.getType());
        parkSubDevVo.setModel(parkDeviceSub.getModel());
        parkSubDevVo.setIp(parkDeviceSub.getIp());
        parkSubDevVo.setPort(parkDeviceSub.getPort());
        parkSubDevVo.setStatus(parkDeviceSub.getStatus());
        parkSubDevVo.setUpdateTime(parkDeviceSub.getUpdateTime());
        parkSubDevVo.setUpdateUser(parkDeviceSub.getUpdateUser());
        parkSubDevVo.setSyncStatus(parkDeviceSub.getSyncStatus());

        String onlineStatus = "未知";
        if (StrUtil.isNotBlank(parkDeviceSub.getOnlineStatus())) {
            JSONObject jsonObject = JSON.parseObject(parkDeviceSub.getOnlineStatus());
            String state = jsonObject.getString("state");
            if (StrUtil.equals("online", state)) {
                onlineStatus = "在线";
            } else {
                onlineStatus = "离线";
            }
        }

        parkSubDevVo.setOnlineStatus(onlineStatus);

        return parkSubDevVo;
    }
}
